package com.zzyl.serve.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzyl.common.constant.CacheConstants;
import com.zzyl.common.utils.StringUtils;
import com.zzyl.serve.domain.NursingLevel;
import com.zzyl.serve.domain.vo.NursingLevelVo;
import com.zzyl.serve.mapper.NursingLevelMapper;
import com.zzyl.serve.service.INursingLevelService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 护理等级Service业务层处理
 *
 * @author Mr.Li
 * @date 2025-05-15
 */
@Service
public class NursingLevelServiceImpl extends ServiceImpl<NursingLevelMapper, NursingLevel> implements INursingLevelService {

    @Autowired
    private NursingLevelMapper nursingLevelMapper;

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    /**
     * 查询护理等级
     *
     * @param id 护理等级主键
     * @return 护理等级
     */
    @Override
    public NursingLevel selectNursingLevelById(Long id) {
        return this.getById(id);
    }

    /**
     * 查询护理等级列表
     *
     * @param nursingLevel 护理等级
     * @return 护理等级
     */
    @Override
    public List<NursingLevel> selectNursingLevelList(NursingLevel nursingLevel) {
        return nursingLevelMapper.selectNursingLevelList(nursingLevel);
    }

    /**
     * 新增护理等级
     *
     * @param nursingLevel 护理等级
     * @return 结果
     */
    @Override
    public int insertNursingLevel(NursingLevel nursingLevel) {
        return this.save(nursingLevel) ? 1 : 0;
    }

    /**
     * 修改护理等级
     *
     * @param nursingLevel 护理等级
     * @return 结果
     */
    @Override
    public int updateNursingLevel(NursingLevel nursingLevel) {
        // 删除缓存中的数据
        removeCache();
        return this.updateById(nursingLevel) ? 1 : 0;
    }

    /**
     * 批量删除护理等级
     *
     * @param ids 需要删除的护理等级主键
     * @return 结果
     */
    @Override
    public int deleteNursingLevelByIds(Long[] ids) {
        // 删除缓存中的数据
        removeCache();
        return this.removeByIds(Arrays.asList(ids)) ? ids.length : 0;
    }

    /**
     * 删除护理等级信息
     *
     * @param id 护理等级主键
     * @return 结果
     */
    @Override
    public int deleteNursingLevelById(Long id) {
        // 删除缓存中的数据
        removeCache();
        return this.removeById(id) ? 1 : 0;
    }

    /**
     * 查询全部护理等级
     *
     * @return 护理等级集合
     */
    @Override
    public List<NursingLevelVo> findAll() {

        /*
        // 第一种写法
        // 1.从Redis缓存中查询护理等级数据
        String CacheJSON = redisTemplate.opsForValue().get(CacheConstants.NURSING_LEVEL_LIST_KEY);
        List<NursingLevelVo> nursingLevelVolist = null;
        if (StringUtils.isNotEmpty(CacheJSON)) {
            // 1.2.将JSON数据转换成对象集合
            nursingLevelVolist = JSON.parseArray(CacheJSON, NursingLevelVo.class);
        }

        // 2.如果没有查询到，则查询查询记录中的全部数据
        if (CollUtil.isEmpty(nursingLevelVolist)) {
            // 2.1.查询状态为1的护理等级信息
            List<NursingLevel> nursingLevelList = this.lambdaQuery().eq(NursingLevel::getStatus, 1).list();

            // 2.2.判断查询的是够为空
            if (CollUtil.isEmpty(nursingLevelList)) {
                return new ArrayList<>();
            }

            // 2.3.把数据映射到视图类vo对象
            nursingLevelVolist = nursingLevelList.stream().map(x -> {
                NursingLevelVo nursingLevelVo = new NursingLevelVo();
                BeanUtils.copyProperties(x, nursingLevelVo);
                return nursingLevelVo;
            }).collect(Collectors.toList());

            // 2.4.将数据保存到Redis缓存中
            redisTemplate.opsForValue().set(CacheConstants.NURSING_LEVEL_LIST_KEY, JSON.toJSONString(nursingLevelVolist));
        }

        // 4.返回数据
        return nursingLevelVolist;
        */

        // 1.查询缓存中的数据
        List<String> stringList = redisTemplate.opsForList().range(CacheConstants.NURSING_LEVEL_LIST_KEY, 0, -1);

        List<NursingLevelVo> nursingLevelVoList = null;

        // 2.判断缓存中的数据是否为空
        if (CollUtil.isNotEmpty(stringList)) {
            nursingLevelVoList = stringList.stream().map(x -> {
                // 2.1.将JSON数据转换成对象集合
                return JSON.parseObject(x, NursingLevelVo.class);
            }).collect(Collectors.toList());
        }
        //  3.缓存中没有数据，则查询数据库
        if (CollUtil.isEmpty(nursingLevelVoList)) {
            // 2.1.查询状态为1的护理等级信息
            List<NursingLevel> nursingLevelList = this.lambdaQuery().eq(NursingLevel::getStatus, 1).list();

            // 2.2.判断查询的是够为空
            if (CollUtil.isEmpty(nursingLevelList)) {
                return new ArrayList<>();
            }

            // 2.3.把数据映射到视图类vo对象
            nursingLevelVoList = nursingLevelList.stream().map(x -> {
                NursingLevelVo nursingLevelVo = new NursingLevelVo();
                BeanUtils.copyProperties(x, nursingLevelVo);
                // 2.4.将数据保存到Redis缓存中
                redisTemplate.opsForList().leftPush(CacheConstants.NURSING_LEVEL_LIST_KEY, JSON.toJSONString(nursingLevelVo));
                return nursingLevelVo;
            }).collect(Collectors.toList());

        }

        // 4.返回数据
        return nursingLevelVoList;
    }

    /**
     * 修改和删除的时候删除Redis中的值防止数据不一致情况
     */
    private void removeCache() {
        redisTemplate.delete(CacheConstants.NURSING_LEVEL_LIST_KEY);
    }
}